/**
*
* Sans mémoire d'activation et avec abort
*  Copyright (c) 2009 Ecole Centrale de Nantes IRCCyN
* see <a href="{@docRoot}/Documentation/task.html"> for more information</a>
* 
* @author: Richard
* @version 3.2
*/

package storm.Tasks;

import java.util.Iterator;

import log.TaskTrace;
import storm.Timer;
import storm.Tasks.Task.Activation;
/**
 * 
 * @author richard
 *
 */
public class PTask_NAM_A extends PTask {
	/**
	 * 
	 */
	
	protected void Abort() {
		super.Abort();
		this.setNormalEnd(false);

	}
	public void resetNormalEnd(){
		if (!this.isNormalEnd() ) { 
			this.setNormalEnd(true);
			this.initRET();
		}
	}
	public void rearmTimer(){
		super.rearmTimer();
		
		
	    Timer timer = new Timer("resetNormalEnd",1,this);
		Kernel.addTimer(timer);
	
		
	}
	
	/**
	 * 
	 */
	public void controleDealine(){
		super.controleDealine();
	}
	/**
	 * 
	 */
	public void inRunning() {
	
		/** Detection de la fin de traitement qui dÃ©passe la pÃ©riode
		 * dÃ©tection d'une exÃ©cution en dehors de la pÃ©riode  
		 */
		if(  (this.ET ==1 ) && (this.getRET()>=0 )) {
			for (int i = this.ActivationFIFO.size()-1 ; i != -1 ; i --){
				Activation E = (Activation) this.ActivationFIFO.get(i);
				if (!E.loseActivation){
						E.setDeb_exec(this.Kernel.getTime());
						break;
				}
			}
		}
		if(  (this.ET > 0 ) && (this.getRET()== 0)) {
			for (int i = this.ActivationFIFO.size()-1 ; i != -1 ; i --){
				Activation E = (Activation) this.ActivationFIFO.get(i);
				if (!E.loseActivation){
					if (E.getDeb_exec()>= 0) {
						E.setEnd_exec(this.Kernel.getTime());
						break;
					}
				}
			}
			
		}
		super.inRunning();
	}
	/**
	 * 
	 */
	public void onTick(){
		super.onTick();

		if (this.isDeadline) {
			Iterator I= this.ActivationFIFO.iterator();
			while (I.hasNext()) {
				Activation A = (Activation) I.next();
				if ( (A.getDeadline() <0) &&(!A.loseActivation)){
					A.setDeadline(this.Kernel.getTime()+1);
					//miss deadline
					if (A.deb_exec<0) this.raiseBlock(this);
					if ((A.end_exec<0)&&(!A.loseActivation)) {
			
						this.Kernel.getLogs().addTrace(new TaskTrace(this.Kernel.getTime(),this, TaskTrace.Type.ABORT,A.getDate()));
						A.setEnd_exec(this.Kernel.getTime());
						A.setAbort(true);
				
						if (this.everRunning) this.raiseBlock(this);
						if (this.isIsrunning()) {
							Timer timer = new Timer("resetRun", 1 ,this);
							Kernel.addTimer(timer);
						}
						else {
							this.setEverRunning(false);
						}
						
						this.Abort();
						
						
					}
 					break;
				}
			}
			this.isDeadline = false;	
		}
		if (this.isnewPeriod) {
			
			Activation A = null;
			for (int i = this.ActivationFIFO.size()-1 ; i != -1 ; i --){
				Activation E = (Activation) this.ActivationFIFO.get(i);
				if (!E.loseActivation){
					
					A = E;
					break;
				}
			}
			/**
			 * récupére activation pas perdue 
			 */
			Activation B = new Activation(this.Kernel.getTime()+1);
			if (A.end_exec<0) {
				B.setLoseActivation(true);
			}
			this.ActivationFIFO.addLast(B);
			this.isnewPeriod = false;	
		}
	}
}
